package com.whiteape.book.ui.activity;

import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.KeyEvent;

import androidx.annotation.Nullable;
import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.Fragment;

import com.whiteape.book.R;
import com.whiteape.book.WaApp;
import com.whiteape.book.model.dto.DownloadTaskBean;
import com.whiteape.book.model.dto.MediaChapterBean;
import com.whiteape.book.model.type.MediaType;
import com.whiteape.book.service.DownloadService;
import com.whiteape.book.ui.adapter.DownloadAdapter;
import com.whiteape.book.ui.base.BaseActivity;
import com.whiteape.book.ui.base.BaseFragment;
import com.whiteape.book.ui.fragment.DownloadFragment;
import com.whiteape.book.utils.StringUtils;

public class DownloadActivity extends BaseActivity {
    DownloadFragment downloadFragment = DownloadFragment.newInstance(MediaType.MEDIA_TYPE_NOVEL);

    ServiceConnection conn;
    DownloadService.IDownloadTask downloadTask;

    @Override
    protected void initView() {
        super.initView();
//        getWindow().getDecorView().setBackgroundColor(WaApp.getInstance().getResources().getColor(R.color.statusBarColor));
        getSupportFragmentManager().beginTransaction()
                .add(R.id.ll_download_content, downloadFragment)
                .commit();

    }

    @Override
    protected int getContentId() {
        return R.layout.activity_download;
    }

    @Override
    protected void setUpToolbar(Toolbar toolbar) {
        super.setUpToolbar(toolbar);
        if (null != getSupportActionBar()) {
            getSupportActionBar().setTitle(StringUtils.getString(R.string.wa_download_activity_title));
        }
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        conn = new ServiceConnection() {
            @Override
            public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
                downloadTask = (DownloadService.IDownloadTask) iBinder;
                downloadTask.setDownloadListener(downloadFragment);
                downloadFragment.finishRefresh(downloadTask.getDownloadTaskBeanList());
                downloadTask.startWork();

            }

            @Override
            public void onServiceDisconnected(ComponentName componentName) {

            }
        };
//        startService(new Intent(this, DownloadService.class));
        bindService(new Intent(this, DownloadService.class), conn, Service.BIND_AUTO_CREATE);
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbindService(conn);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (downloadFragment.onKeyDown(keyCode, event)) {
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
